package com.ezjie.ielts.module_word;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.model.TestOfflineBean;
import com.ezjie.ielts.module_word.adapter.OfflineAdapter;
import com.ezjie.ielts.service.ConfigUtil;
import com.ezjie.ielts.service.DownloadWordService;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.util.WordHomeDataUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

/**
 * 离线管理界面
 * 
 * @author renyangyang
 *
 */
@ContentView(R.layout.layout_offline_manage)
public class OfflineManageActivity extends BaseFragmentActivity {
	private static final int UNDOWNLOAD = 0;
	private static final int DOWNLOADING = 1;
	private static final int DOWNLOADED = 2;
	private int status;

	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;
	@ViewInject(R.id.tv_topbar_title)
	private TextView titleText;
	@ViewInject(R.id.tv_word_update)
	private TextView tv_word_update;
	@ViewInject(R.id.iv_download)
	private ImageView iv_download;
	@ViewInject(R.id.pb_loading)
	private ProgressBar pb_loading;
	@ViewInject(R.id.rl_word_download)
	private RelativeLayout rl_word_download;
	@ViewInject(R.id.lv_offline)
	private ListView lv_offline;
	private OfflineAdapter mAdapter;
	private List<TestOfflineBean> mList;

	private DownloadReceiver downloadReceiver;// 下载的广播

	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		mList = new ArrayList<TestOfflineBean>();
		TestOfflineBean testOfflineBean = new TestOfflineBean();
		testOfflineBean.setName("阅读练习");
		testOfflineBean.setTitle(true);

		TestOfflineBean testOfflineBean1 = new TestOfflineBean();
		testOfflineBean1.setName("阅读离线包");
		testOfflineBean1.setTitle(false);

		TestOfflineBean testOfflineBean2 = new TestOfflineBean();
		testOfflineBean2.setName("听力练习");
		testOfflineBean2.setTitle(true);

		TestOfflineBean testOfflineBean3 = new TestOfflineBean();
		testOfflineBean3.setName("听力离线包");
		testOfflineBean3.setTitle(false);
		mList.add(testOfflineBean);
		mList.add(testOfflineBean1);
		mList.add(testOfflineBean2);
		mList.add(testOfflineBean3);

		downloadReceiver = new DownloadReceiver();
		registerReceiver(downloadReceiver, new IntentFilter(ConfigUtil.WORD_ACTION_DOWNLOADING));

		init();
		mContext = this;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_OFFLINE_MANAGER);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_OFFLINE_MANAGER);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (downloadReceiver != null) {
			unregisterReceiver(downloadReceiver);
		}
	}

	private void init() {
		iv_topbar_back.setOnClickListener(this);
		titleText.setText(R.string.offline_manage_title);
		rl_word_download.setOnClickListener(this);
		lv_offline.setVisibility(View.GONE);
		mAdapter = new OfflineAdapter(this);
		mAdapter.setList(mList);
		lv_offline.setAdapter(mAdapter);

		// 判断是否有更新
		boolean wordHaveUpdate = WordHomeDataUtil.getWordHaveUpdate(this);
		if (wordHaveUpdate) {
			setUnDownload();
		} else {
			setDownloaded();
		}

	}

	private void setUnDownload() {
		if (iv_download != null && tv_word_update != null) {
			iv_download.setVisibility(View.VISIBLE);
			pb_loading.setVisibility(View.INVISIBLE);
			iv_download.setImageResource(R.drawable.offline_update);
			tv_word_update.setTextColor(Color.rgb(255, 178, 0));
			status = UNDOWNLOAD;
		}
	}

	public void setDownloaded() {
		if (iv_download != null && tv_word_update != null) {
			iv_download.setVisibility(View.VISIBLE);
			pb_loading.setVisibility(View.INVISIBLE);
			iv_download.setImageResource(R.drawable.offline_downloaded);
			tv_word_update.setTextColor(Color.rgb(223, 223, 223));
			status = DOWNLOADED;
		}
	}

	public void setDownloading() {
		if (iv_download != null && tv_word_update != null) {
			iv_download.setVisibility(View.INVISIBLE);
			pb_loading.setVisibility(View.VISIBLE);
			iv_download.setImageResource(R.drawable.offline_downloaded);
			tv_word_update.setTextColor(Color.rgb(255, 178, 0));
			status = DOWNLOADING;
		}
	}

	private class DownloadReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent != null) {
				int status = intent.getIntExtra("status", ConfigUtil.ERROR);
				switch (status) {
				case ConfigUtil.ERROR:
					// 下载错误 json异常或者 接口异常
					setUnDownload();
					break;
				case ConfigUtil.DOWNLOADING:
					// 下载中
					setDownloading();
					break;
				case ConfigUtil.DOWNLOADED:
					// 下载完成
					setDownloaded();
					break;

				default:
					break;
				}
			}

		}

	}

	@Override
	public void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.iv_topbar_back:
			finish();
			break;
		case R.id.rl_word_download:
			if (status == UNDOWNLOAD) {
				Intent intent = new Intent(mContext, DownloadWordService.class);
				intent.putExtra("request", true);
				startService(intent);
			}
			break;
		default:
			break;
		}
	}

}
